package com.vpen.account.demo.utils;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 描述
 *
 * @author 韦鹏
 * @date 2022/8/15 17:33
 */
public class SpringUtils {


    /***
     * 获取当前request
     *
     * @author 韦鹏
     * @date 2022/8/15 17:46
     * @return javax.servlet.http.HttpServletRequest
     **/
    public static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (null == requestAttributes) {
            throw new NullPointerException();
        }
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        return servletRequestAttributes.getRequest();
    }


    /**
     * 获取当前线程某个参数
     *
     * @param name 参数名
     * @return java.lang.String
     * @author 韦鹏
     * @date 2022/8/15 17:45
     **/
    public static String getParameter(String name) {
        HttpServletRequest request = getRequest();
        return request.getParameter(name);
    }
}
